import { request } from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mail: "",
    username: "",
    password: "",
    consistence: true,
    nickname: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  handleMail(e) {
    this.setData({
      mail: e.detail.value
    })
  },

  handleMailBlur(e) {

  },

  handleUsername(e) {
    this.setData({
      username: e.detail.value
    })
  },

  handleUsernameBlur(e) {

  },

  handlePassword(e) {
    this.setData({
      password: e.detail.value
    })
  },

  handlePasswordBlur(e) {

  },

  handleAgainBlur(e) {
    const { value } = e.detail;
    if (value == this.data.password) {
      this.setData({
        consistence: true
      })
    } else {
      this.setData({
        consistence: false
      })
    }
  },

  handleNickname(e) {
    this.setData({
      nickname: e.detail.value
    })
  },

  async handleConfirm(e) {
    const data = {
      user_name: this.data.username,
      nick_name: this.data.nickname,
      user_code: this.data.password,
      user_email: this.data.mail
    }
    var that = this;
    wx.request({
      url: 'http://47.94.130.28:8000/user/register/',
      data: data,
      method: "POST",
      success (res) {
        const valid = res.data.valid;
        const message = res.data.message;
        if (valid) {
          // Directly login and jump to success page
          const data = {
            user_name: that.data.username,
            user_code: that.data.password
          }
          wx.request({
            url: 'http://47.94.130.28:8000/user/login/',
            data: data,
            method: "POST",
            success (res) {
              const user_info = res.data;
              wx.setStorageSync('user_info', user_info)
              wx.reLaunch({
                url: '/pages/success/success?user_name=' + user_info.user_name
              })
            }
          })
        } else {
          wx.showModal({
            title: "注册失败",
            content: message,
            showCancel: false
          })
        }
      }
    })
    // const user_info = {
    //   valid: true,
    //   uid: "12345678",
    //   user_name: "bujiangwude",
    //   nick_name: "TaichiMa",
    //   image_src: "/static/g1.jpg",
    //   user_email: "mabaoguo17@mails.tsinghua.edu.cn",
    //   user_phone: "18012345678",
    //   user_vxid: "wx_awkl8h1b35k",
    //   user_status: true
    // }
    // wx.setStorageSync('user_info', user_info)
    // wx.reLaunch({
    //   url: '/pages/success/success?user_name=' + user_info.user_name
    // })
  }
})